package com.yunxin.core.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.core.application.command.page.WeightPageCommand;
import com.yunxin.core.application.service.SysUserWeightService;
import com.yunxin.core.domain.model.weight.SysTeamWeight;
import com.yunxin.core.domain.model.weight.SysUserWeight;
import com.yunxin.core.domain.model.weight.SysUserWeightDto;
import com.yunxin.core.domain.model.weight.SysWeightDelete;
import com.yunxin.core.infra.persistence.mapper.weight.SysUserWeightMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
@Service
public class SysUserWeightServiceImpl extends ServiceImpl<SysUserWeightMapper, SysUserWeight> implements SysUserWeightService {

    @Override
    public IPage<SysUserWeight> getUserWeightPage(WeightPageCommand command) {
        Page<SysUserWeight> page = new Page<>(command.getPageNum(), command.getPageSize());
        return baseMapper.selectPage(page, new QueryWrapper<SysUserWeight>()
                .eq("merchant_id", command.getMerchantId())
                .eq("is_del", SysUserWeight.ON)
                .ge(command.getStartTime()!=null,"create_time", command.getStartTime())
                .le(command.getEndTime()!=null,"create_time", command.getEndTime()).orderByDesc("id"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveUserWeigh(SysUserWeightDto command) {
        // 先查
        SysUserWeight weight = baseMapper.selectOne(new QueryWrapper<SysUserWeight>()
                .eq("weight_name", command.getWeightName())
                .eq("is_del", SysTeamWeight.ON)
                .eq("merchant_id", command.getMerchantId()));
        if(Objects.nonNull(weight)){
            throw new BusinessException("权重名称重复");
        }
        SysUserWeight userWeight = new SysUserWeight();
        BeanUtil.copyProperties(command,userWeight);
        userWeight.setIsDel(SysTeamWeight.ON);
        userWeight.setStatus(SysTeamWeight.ON);
        userWeight.setCreateTime(new Date());
        try {
            baseMapper.insert(userWeight);
        } catch (Exception e) {
            throw new BusinessException("添加个人权重信息异常");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editUserWeigh(SysUserWeightDto command) {
// 先查
        SysUserWeight weight = baseMapper.selectOne(new QueryWrapper<SysUserWeight>()
                .eq("weight_name", command.getWeightName())
                .eq("is_del", SysTeamWeight.ON)
                .eq("merchant_id", command.getMerchantId()));
        if(Objects.nonNull(weight) && !weight.getId().equals(command.getId())){
            throw new BusinessException("权重名称重复");
        } try {
            SysUserWeight userWeight = new SysUserWeight();
            BeanUtil.copyProperties(command,userWeight);
            userWeight.setUpdateTime(new Date());
            baseMapper.updateById(userWeight);
        } catch (Exception e) {
            throw new BusinessException("修改用户权重信息异常");
        }
        return true;
    }

    @Override
    public Boolean deleteUserWeight(SysWeightDelete command) {
        command.getIds().forEach(this::accept);
        return true;
    }

    @Override
    public Boolean weightSwitch(SysUserWeight command) {
        // 先查
        SysUserWeight weight = baseMapper.selectOne(new QueryWrapper<SysUserWeight>().eq("id", command.getId()));
        if(Objects.isNull(weight)){
            throw new BusinessException("权重不存在");
        }
        weight.setStatus(command.getStatus());
        try {
            baseMapper.updateById(weight);
        } catch (Exception e) {
            throw new BusinessException("修改用户权重开关异常");
        }
        return true;
    }

    @Override
    public List<SysUserWeight> getUserWeightList(Long merchantId) {
        return baseMapper.selectList(new QueryWrapper<SysUserWeight>()
                .eq("merchant_id", merchantId)
                .eq("is_del", SysUserWeight.ON)
                .eq("status", SysUserWeight.ON));
    }

    private void accept(Integer id) {
        // 先查
        SysUserWeight weight = baseMapper.selectOne(new QueryWrapper<SysUserWeight>().eq("id", id));
        if(Objects.isNull(weight)){
            throw new BusinessException("权重不存在");
        }
        weight.setIsDel(SysUserWeight.OFF);
        weight.setUpdateTime(new Date());
        try {
            baseMapper.updateById(weight);
        } catch (Exception e) {
            throw new BusinessException("删除个人权重信息异常");
        }

    }
}




